package com.haifeng.questionbankcrud.dao.impl;

import com.haifeng.questionbankcrud.dao.TeacherDao;
import com.haifeng.questionbankcrud.pojo.Teacher;
import com.haifeng.questionbankcrud.util.DBUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TeacherDaoImpl implements TeacherDao {
    private Connection conn = null;
    private Statement st = null;
    private ResultSet sr = null;

    public TeacherDaoImpl() {
//
        try {
            /**
             * 适用druid作为连接池
             */
            conn = DBUtils.getConnection();
            st = conn.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Override
    public List<Teacher> queryUserByNameAndPassword(Teacher teacher) {
        ArrayList<Teacher> teacherList = new ArrayList<>();
        String sql = "select * from teacher where t_name=?and password=?";
        try {
            PreparedStatement preparedStatement = DBUtils.getConnection().prepareStatement(sql);
            preparedStatement.setString(1, teacher.getT_name());
            preparedStatement.setString(2, teacher.getPassword());
            ResultSet resultSet = preparedStatement.executeQuery();


            while (resultSet.next()) {
                //把查询查询出来的数据封装到user中并添加到list里
                Teacher item = new Teacher();
                item.setT_id(resultSet.getInt("t_id"));
                item.setT_name(resultSet.getString("t_name"));
                item.setPassword(resultSet.getString("password"));
                teacherList.add(item);


            }
            return teacherList;

        } catch (SQLException e) {
            e.printStackTrace();
            return teacherList;
        }

    }

    @Override
    public List<Teacher> findAll() {
        ArrayList<Teacher> teacherList = new ArrayList<>();
        String sql = "select * from teacher;";
        try {
            PreparedStatement preparedStatement = DBUtils.getConnection().prepareStatement(sql);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                //把查询查询出来的数据封装到user中并添加到list里
                Teacher item = new Teacher();
                item.setT_id(resultSet.getInt("t_id"));
                item.setT_name(resultSet.getString("t_name"));
                item.setPassword(resultSet.getString("password"));
                teacherList.add(item);


            }
            return teacherList;

        } catch (SQLException e) {
            e.printStackTrace();
            return teacherList;
        }

    }

    /**
     * 添加教师用户
     * 返回有值则添加成功
     *
     * @param teacher
     * @return
     */
    @Override
    public int addTeacher(Teacher teacher) {
        int msg = 0;
        String sql = "insert into teacher values (null,?,?)";
        PreparedStatement preparedStatement = null;
        try {
            System.out.println(teacher);
            preparedStatement = conn.prepareStatement(sql);
            preparedStatement.setString((int) 1, teacher.getT_name());
            preparedStatement.setString((int) 2, teacher.getPassword());
            msg = preparedStatement.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
            return msg;
        }

        return msg;

    }

}
